/**
 * Copyright (c) 2012 Nokia Corporation.
 */

#include "geopermissionhandler.h"

/*!
  \class GeoPermissionHandler
  \brief Listens to permission request from the QWebPage and grants access if
         the permission is QWebPage::Geolocation.
*/

GeoPermissionHandler::GeoPermissionHandler(QObject *parent)
    : QObject(parent)
{
}

GeoPermissionHandler::~GeoPermissionHandler()
{
}

QDeclarativeWebView *GeoPermissionHandler::view() const
{
    return m_webView;
}

void GeoPermissionHandler::setView(QDeclarativeWebView *view)
{
    m_webView = view;

#ifndef Q_WS_SIMULATOR
    if (m_webView) {
        qDebug() << "Listening to Geolocation permission requests...";
        connect(m_webView->page(), SIGNAL(featurePermissionRequested(QWebFrame*, QWebPage::Feature)),
                this, SLOT(handlePermissionRequest(QWebFrame*, QWebPage::Feature)));
    }
#endif
}

#ifndef Q_WS_SIMULATOR
/*!
  Handles the feature permission requests from web pages. Grants permission to
  \a frame if the type of \a feature is QWebPage::GeoLocation.
*/
void GeoPermissionHandler::handlePermissionRequest(QWebFrame *frame,
                                                   QWebPage::Feature feature)
{
    if (feature == QWebPage::Geolocation) {
        qDebug() << "Granting permission for QWebPage::Geolocation feature.";
        m_webView->page()->setFeaturePermission(frame, feature,
                                                QWebPage::PermissionGrantedByUser);
    }
}
#endif
